#include <bits/stdc++.h>

using namespace std;

int main()
{
    int bag_weight, n;
    cin >> bag_weight >> n;
    vector<int> weight(n, 0);
    vector<int> value(n, 0);
    vector<int> nums(n, 0);
    for (int i = 0; i < n; i++)
    {
        cin >> weight[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> value[i];
    }
    for (int i = 0; i < n; i++)
    {
        cin >> nums[i];
    }
    vector<int> dp(bag_weight + 1, 0);
    for (int i = 0; i < n; i++)
    {
        for (int j = bag_weight; j >= weight[i]; j--)
        {
            for (int k = 1; k <= nums[i] && j - k * weight[i] >= 0; k++)
            {
                dp[j] = max(dp[j], dp[j - k * weight[i]] + k * value[i]);
            }
        }
    }
    cout << dp[bag_weight] << endl;
    return 0;
}